package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomattrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Override
    public IPage<RoomItemVo> getPageList(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return baseMapper.getPageList(roomItemVoPage,queryVo);
    }

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        Long roomId = roomSubmitVo.getId();
        //调用自带的MyBatis Plus的保存或更新的方法
        saveOrUpdate(roomSubmitVo);
        //判断是添加还是更新
        if (roomId != null) {
            //证明在更新，删除中间表中的数据
            //1.根据房间id和图片类型删除房间图片
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, roomId));
            //2.根据房间id删除属性信息
            roomattrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomId));
            //3.根据房间id删除配套信息
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomId));
            //4根据房间id删除标签信息
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomId));
            //5.根据房间id删除支付方式
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomId));
            //6.根据房间id删除可选租期
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomId));

        }
        //向中间表插入数据
        //1.保存新的graphInfoList
  List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            //批量保存
            graphInfoService.saveBatch(graphInfoList);
        }
        //2保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds){
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomattrValueService.saveBatch(roomAttrValueList);
        }
        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            //创建集合
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            //遍历
            for (Long facilityInfoId : facilityInfoIds){
                //创建RoomFacility对象
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
               roomFacilityList.add(roomFacility);
            }
            //批量保存
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            //遍历
            for (Long labelInfoId : labelInfoIds){
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //5.保存房间的支付方式

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            //创建集合
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            //遍历
            for (Long paymentTypeId : paymentTypeIds) {
                //创建RoomPaymentType对象
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                //放入集合中
                roomPaymentTypeList.add(roomPaymentType);
            }
            //批量保存
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds){
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public void deleteRoomInfoById(Long roomId) {
        //根据房间id查询租约表看当前房间是否被入住
       Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, roomId).in(LeaseAgreement::getStatus, 2, 5, 7));

       if (count > 0){
           //该房间有人，不能删
           throw new LeaseException(ResultCodeEnum.HAS_PERSON);
       }
        //调用MyBats Plus自带的方法删除房间信息
        removeById(roomId);

        //删除中间表的数据
        //1.根据房间id和图片类型删除房间图片
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, roomId));
        //2.根据房间id删除属性信息
        roomattrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomId));
        //3.根据房间id删除配套信息
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomId));
        //4根据房间id删除标签信息
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomId));
        //5.根据房间id删除支付方式
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomId));
        //6.根据房间id删除可选租期
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomId));

    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        //调用MyBats Plus自带的方法查询房间信息
      RoomInfo roomInfo = getById(id);
      //查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //查询中间表的数据
        //1.查询房间的图片
        List<GraphVo> graphVoList = graphInfoMapper.getGraphVoByRoomIdAndItemType(id,ItemType.ROOM);
        //2.查询属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVoListByRoomId(id);
        //3.查询配置信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoListByRoomId(id);
        //4.查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoListByRoomId(id);
        //5.查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeListByRoomId(id);
        //6.查询可选租期
        List<LeaseTerm> leaseTermList = labelInfoMapper.getleaseTermListByRoomId(id);
        //创建RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //复制房间的属性值
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //设置公寓信息
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }
}




